<?php

class Model_question extends CI_Model {
    
    function __construct() {
        parent::__construct();
    }

    public function getQuestions() {
        $collection = $this->mongo_db->db->selectCollection('questions');
        $question = $collection->find(array('advance'=>array('$ne'=>true)));     
        return $question;
    }
    public function getQuestionsArr($arr) {    	
    	$collection = $this->mongo_db->db->selectCollection('questions');
        $questions = $collection->find(array("_id" => array('$in' => $arr)));
        return $questions;
    }
    public function getAdvanceQuestions() {
        $collection = $this->mongo_db->db->selectCollection('questions');
        $questions = $collection->find(array('advance'=>true));
        return $questions;
    }
    public function getAdvanceQuestion($id) {
        $collection = $this->mongo_db->db->selectCollection('questions');
        $id = new MongoId($id);
        $question = $collection->findOne(array('_id'=>$id));
        return $question;
    }
    public function getQsByEvent($idEvent) {
        $collection = $this->mongo_db->db->selectCollection('questions');
        $question = $collection->find(array('events'=>$idEvent));
        return $question;
    }

    public function insert_question($question, $cat, $answer)
    {
        $collection = $this->mongo_db->db->selectCollection('questions');
        $id = new  MongoId();
        $id = (string)$id;
        $data = array(
            '_id' => $id,
            'question' => $question,
            'cat' => $cat,
            'answer' => $answer,
        );
        $insertOptions = array(
            'safe'    => true,
            'fsync'   => true,
            'timeout' => 10000
        );
        try {
            $result = $collection->insert($data);
            return 'Thanh cong';
        }
        catch(MongoCursorException $mce) {
            return 'Thêm câu hỏi thất bại';
        }
    } 

    // ====================================================================================================  
    // ====================================================================================================  
    // ====================================================================================================  
    // ====================================================================================================  

    public function get_opt_question()
    {
        $collection = $this->mongo_db->db->selectCollection('choosetest');
        $questions = $collection->find();
        return $questions;
    }   

    public function insert_opt_question($data)
    {
        $collection = $this->mongo_db->db->selectCollection('choosetest');
        try {
            $result = $collection->insert($data);
            return 'success';
        }
        catch(MongoCursorException $mce) {
            return 'fail';
        }
    }   
}
?>